import Util.ExcelUtil;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * Created by Administrator on 2017/2/12.
 */
public class AnnotationReplace {

    private JButton selectexcel;
    private JComboBox templatesheet;
    private JButton startbutton;
    private JComboBox datesheet;
    private JButton selectword;
    private JPanel Panel1;
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextPane textPane1;

    private String selectpath;
    private String excelfilepath;
    private String wordfilepath;

    public static void main(String[] args) {
        JFrame frame = new JFrame("AnnotationReplace");
        frame.setContentPane(new AnnotationReplace().Panel1);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public AnnotationReplace() {
        selectexcel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc;
                if (selectpath == "" || selectpath == null) {
                     jfc = new JFileChooser();
                }
                else{
                     jfc = new JFileChooser(selectpath);
                }
                jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );
                jfc.showDialog(new JLabel(), "选择");
                File file=jfc.getSelectedFile();
                excelfilepath = file.getAbsolutePath();
                try {
                    selectpath = file.getCanonicalPath();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                System.out.println("文件:"+file.getAbsolutePath());
                String fileType = excelfilepath.substring(excelfilepath.lastIndexOf(".") + 1, excelfilepath.length());
                if (fileType.equals("xls") || fileType.equals("xlsx")) {
                    textPane1.setText("文件:"+excelfilepath);
                }
                else{
                    textPane1.setText("只支持xls，与xlsx的文件");
                    return;
                }
                System.out.println(jfc.getSelectedFile().getName());
                List<String> sheetList ;
                try {
                    sheetList = ExcelUtil.getSheetList(excelfilepath);
                    DefaultComboBoxModel<String> templatesheetmodel = new DefaultComboBoxModel<String>();
                    DefaultComboBoxModel<String> datesheetmodel = new DefaultComboBoxModel<String>();
                    for (String s :
                            sheetList) {
                        templatesheetmodel.addElement(s);
                        datesheetmodel.addElement(s);
                    }
                    templatesheet.setModel(templatesheetmodel);
                    templatesheet.updateUI();
                    datesheet.setModel(datesheetmodel);
                    datesheet.updateUI();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });
        selectword.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jfc;
                if (selectpath == "" || selectpath == null) {
                    jfc = new JFileChooser();
                }
                else{
                    jfc = new JFileChooser(selectpath);
                }
                jfc.setFileSelectionMode(JFileChooser.FILES_ONLY );
                jfc.showDialog(new JLabel(), "选择");
                File file=jfc.getSelectedFile();
                wordfilepath = file.getAbsolutePath();
                try {
                    selectpath = file.getCanonicalPath();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                System.out.println("文件:"+file.getAbsolutePath());
                String fileType = wordfilepath.substring(wordfilepath.lastIndexOf(".") + 1, wordfilepath.length());
                if (fileType.equals("docx")) {
                    textPane1.setText("文件:"+wordfilepath);
                }
                else{
                    textPane1.setText("只支持docx的文件");
                    return;
                }
            }
        });


        startbutton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(excelfilepath == "" || excelfilepath == null){
                    textPane1.setText("请选择excel文件");
                    return;
                }
                if(wordfilepath == "" || wordfilepath == null){
                    textPane1.setText("请选择word文件");
                    return;
                }
                textPane1.setText("生成中...");
                System.out.println();
                System.out.println();

                String Msg = null;
                try {
                    Msg = WordReplace.Run(excelfilepath,templatesheet.getSelectedItem().toString(),datesheet.getSelectedItem().toString(),wordfilepath);
                } catch (IOException e1) {
                    textPane1.setText(e1.getMessage());
                }
                textPane1.setText(Msg);
            }
        });
    }

    private void createUIComponents() {
        // TODO: place custom component creation code here
    }
}
